package com.iaiai.imail.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.iaiai.imail.dao.InboxDao;
import com.iaiai.imail.entity.Email;
import com.iaiai.imail.entity.Inbox;
import com.iaiai.imail.helper.DatabaseOpenHelper;

/**
 * 
 * <p>
 * Title: InboxDaoImpl.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-11-2 上午9:33:14
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class InboxDaoImpl implements InboxDao {
	
	public void insertInbox(Inbox inbox){
		SQLiteDatabase sqlite = DatabaseOpenHelper.getInstance().getWritableDatabase();
		sqlite.execSQL(
				"insert into INBOX(" +
						"EID" +
						",MID"+
						",TITLE" +
						",CONTENT" +
						",FORM" +
						",TOADDRESS" +
						",CC" +
						",BCC" +
						",SEND_DATE" +
						",REPLYSIGN" +
						",ATTACH"+
						",CONTENTTYPE"+
						",DISPOSITION"+
						",DESCRIPTION"+
						",SIZE"+
						",HEADER"+
						",CREATE_DATE"+
						",READ"+
						") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new Object[]{
						inbox.getEid(),
						inbox.getMid(),
						inbox.getTitle(),
						inbox.getContent(),
						inbox.getForm(),
						inbox.getToAddress(),
						inbox.getCc(),
						inbox.getBcc(),
						inbox.getSendDate(),
						inbox.getReplysign(),
						inbox.getAttach(),
						inbox.getContentType(),
						inbox.getDisposition(),
						inbox.getDescription(),
						inbox.getSize(),
						inbox.getHeader(),
						inbox.getCreateDate(),
						inbox.getRead()
						});
		sqlite.close();
	}
	
	public boolean isMessage(String mid){
		boolean bol = false;
		
		SQLiteDatabase sqlite = DatabaseOpenHelper.getInstance().getReadableDatabase();
		Cursor cursor = sqlite.rawQuery("select * from INBOX where MID=?", new String[]{mid});
		if(cursor.moveToNext()){
			bol = true;
		}
		
		cursor.close();
		sqlite.close();
		
		return bol;
	}
	
	public List<Inbox> queryInboxByEmail(Integer eid){
		List<Inbox> list = new ArrayList<Inbox>();
		
		SQLiteDatabase sqlite = DatabaseOpenHelper.getInstance().getReadableDatabase();
		Cursor cursor = sqlite.rawQuery("select * from INBOX where EID="+eid+" order by SEND_DATE desc",null);
		
		while(cursor.moveToNext()){  
            list.add(getEntity(cursor));
        }
		
		cursor.close();
		sqlite.close();
		return list;
	}
	
	private Inbox getEntity(final Cursor cursor){
		Inbox inbox = new Inbox();
		inbox.setId(cursor.getInt(cursor.getColumnIndex("ID")));
		inbox.setEid(cursor.getInt(cursor.getColumnIndex("EID")));
		inbox.setMid(cursor.getString(cursor.getColumnIndex("MID")));
		inbox.setTitle(cursor.getString(cursor.getColumnIndex("TITLE")));
		inbox.setContent(cursor.getString(cursor.getColumnIndex("CONTENT")));
		inbox.setForm(cursor.getString(cursor.getColumnIndex("FORM")));
		inbox.setToAddress(cursor.getString(cursor.getColumnIndex("TOADDRESS")));
		inbox.setCc(cursor.getString(cursor.getColumnIndex("CC")));
		inbox.setBcc(cursor.getString(cursor.getColumnIndex("BCC")));
		inbox.setSendDate(cursor.getString(cursor.getColumnIndex("SEND_DATE")));
		inbox.setReplysign(cursor.getInt(cursor.getColumnIndex("REPLYSIGN")));
		inbox.setAttach(cursor.getInt(cursor.getColumnIndex("ATTACH")));
		inbox.setContentType(cursor.getString(cursor.getColumnIndex("CONTENTTYPE")));
		inbox.setDisposition(cursor.getString(cursor.getColumnIndex("DISPOSITION")));
		inbox.setDescription(cursor.getString(cursor.getColumnIndex("DESCRIPTION")));
		inbox.setSize(cursor.getInt(cursor.getColumnIndex("SIZE")));
		inbox.setHeader(cursor.getString(cursor.getColumnIndex("HEADER")));
		inbox.setCreateDate(cursor.getString(cursor.getColumnIndex("CREATE_DATE")));
		inbox.setRead(cursor.getInt(cursor.getColumnIndex("READ")));
        return inbox;
	}

}
